package com.itheima.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.bean.Setmeal;
import com.itheima.common.PageParam;
import com.itheima.common.R;
import com.itheima.dto.SetmealDto;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/setmeal")
@RestController
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /**
     * 添加套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R add(@RequestBody SetmealDto setmealDto){

        //1. 调用service
        int row = setmealService.add(setmealDto);

        //2. 返回结果
        if(row > 0){
            return R.success("添加套餐成功！");
        }else{
            return R.error("添加套餐失败！");
        }

    }

    /**
     * 分页查询套餐
     * @param pageParam
     * @return
     */
    @GetMapping("/page")
    public R findPage(PageParam pageParam){

        try {
            //1. 调用service
            IPage<SetmealDto> page = setmealService.findByPage(pageParam);

            //2. 返回结果
            return R.success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("分页查询套餐失败！");
        }

    }


    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    public R delete(@RequestParam List<Long> ids){

        int delete = setmealService.delete(ids);

        if(delete > 0){
            return R.success("删除套餐成功！");
        }else{
            return R.error("删除套餐失败！");
        }

    }


    /**
     * 根据分类来获取该分类下的套餐数据
     * @param categoryId
     * @param status
     * @return
     */
    @GetMapping("/list")
    public R list(Long categoryId , Integer status){

        List<Setmeal> list = setmealService.findByCategoryId(categoryId, status);
        return R.success(list);

    }
	  /**
     * 对菜品批量或者是单个 进行停售或者是起售
     * @return
     */
    @PostMapping("/status/{status}")
//注解才能获取到参数
    public R<String> status(@PathVariable("status") Integer status,@RequestParam List<Long> ids){
        setmealService.updateSetmealStatusById(status,ids);
        return R.success("售卖状态修改成功");
    }

    /**
     * 回显套餐数据：根据套餐id查询套餐
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> getData(@PathVariable Long id){
        SetmealDto setmealDto = setmealService.getDate(id);
        return R.success(setmealDto);
    }
//套餐修改之后提交
    @PutMapping
    public R<String> put(@RequestBody SetmealDto setmealDto){
        setmealService.updateWithDish(setmealDto);
        if (setmealDto==null){
            return R.error("修改失败");
        }
        return R.success("修改成功");
    }
}

